<?php
namespace app\admin\controller;

use app\admin\model\UserType as UserTypeModel;
use Request;

/**
 * 用户分信息
 */
class UserType extends Base
{
    /*
    显示分类信息列表
     */
    public function index($json = '')
    {
        $data = UserTypeModel::select();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /*
    信息添加
     */
    public function add($value = '')
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\UserType');
            if (true !== $result) {
                return $this->error($result);
            }

            $data = UserTypeModel::create($post);
            if (!empty($data)) {
                return $this->success('添加成功', 'index');
            }
        }
        return $this->fetch();
    }

    /*
    信息编辑
     */
    public function edit()
    {
        $id    = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new UserTypeModel;
        if ($this->request->isPost()) {
            $post = $this->request->post();
            //是提交操作
            $result = $this->validate($post, 'app\admin\validate\UserType');
            if (true !== $result) {
                return $this->error($result);
            }

            $model = new UserTypeModel;
            $model = $model->allowField(true)->save($post, $id);

            if ($model) {
                return $this->success('修改成功', 'index');
            } else {
                return $this->error('修改失败');
            }

        }

        $info = UserTypeModel::get($id);
        if (empty($info)) {return $this->error('id参数错误');}
        //var_dump($user);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /*
    信息删去
     */
    public function delete($value = '')
    {
        $id   = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data = UserTypeModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        $strlog = $data->name;
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'index', '', 1);
            }
        }
        return $this->error('删去失败');
    }
    /**
     * 快捷操作
     * @return [type] [description]
     */
    public function quickedit()
    {
        //$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {
            if (input('?post.status')) {
                $data = ['id' => Request::post('id/d'), 'status' => Request::post('status')]; //状态
            } else if (input('?post.remark')) {
                $data = ['id' => Request::post('id/d'), 'remark' => Request::post('remark')]; //菜单
            }
            $model = UserTypeModel::update($data);
            if (!empty($model)) {

                return $this->success('修改成功');
            } else {
                return $this->error('修改失败');
            }
        }
    }
}
